var express = require('express');
var router = express.Router();
var uglobal = require('../lib/global');
var mysql = require('../lib/mysql');
var muser = require('../lib/model/user');
var events = require('events');
/* GET users listing. */
//router.get('/', function(req, res, next) {
//  res.send('welcome to user center');
//});
router.get("/login",function(req, res,next){
  res.render("users/login", {title: uglobal.title});
});
router.get("/register",function(req, res,next){
  res.render("users/register", {title: uglobal.title});
});
//登录
var eventEmitter = new events.EventEmitter();
router.post("/login",function(req, res,next){
  var pinfo = req.body;
  var   userName = pinfo.userName,
        password = pinfo.password;
    //清除监听
    eventEmitter.removeAllListeners('loginok');
    eventEmitter.removeAllListeners('loginfail');

    eventEmitter.on("loginok",function(user){
        var quikling = req.query.quikling;
        res.cookie("loginname", user.uname);
        res.cookie("loginuid", user.uid);
        var users = uglobal.checkLogin(req);
        res.render('users/login', {title: uglobal.title, success: true, user: users});
    });
    eventEmitter.on("loginfail",function(){
        res.render('users/login',{title:uglobal.title,errors:true,error:"登录失败！"});
    });
    muser.checkUser(userName,password,eventEmitter);
    //mysql.connect();
  //var sql = "SELECT * FROM q_user WHERE uname = ? AND password = ?";
  //  mysql.query(sql,[userName,password],function(err,rows,fields){
  //if(rows && rows.length > 0){
  //  res.cookie("loginname",rows[0].uname);
  //  res.cookie("loginuid",rows[0].uid);
  //  res.render('users/login',{title:uglobal.title,success:true,user:{"loginname":rows[0].uname,
  //    "loginuid":rows[0].uid}});
  //} else {
  //  res.render('users/login',{title:uglobal.title,errors:{error:"登录失败！"}});
  //}

  //----------------加salt
  //var sql = "SELECT * FROM q_user WHERE uname = ?";
  //mysql.query(sql,[userName],function(err,rows,fields){
  //    if(rows && rows.length > 0){
  //      var  salt = rows[0].salt;
  //        var user = uglobal.checkLogin(req);
  //      var newpassword = "";
  //
  //      if(salt != ''){
  //         newpassword = uglobal.getSignPassword(password, salt);
  //      } else {
  //        newpassword = password;
  //      }
  //      if(rows[0].password == newpassword){
  //        res.cookie("loginname",rows[0].uname);
  //        res.cookie("loginuid",rows[0].uid);
  //        res.render('users/login',{title:uglobal.title,success:true,user:user});
  //      } else {
  //        res.render('users/login',{title:uglobal.title,errors:{error:"登录失败！"}});
  //      }
  //    }
  //    mysql.close();
  //  });
});
//注册
router.post("/register",function(req, res,next){
  var pinfo = req.body;
   var   userName = pinfo.userName,
      password = pinfo.password,
         email = pinfo.email,
      realName = pinfo.realname,
        gender = pinfo.gender;
  var salt = uglobal.setSalt();
    var user = uglobal.checkLogin(req);
  var newpass = uglobal.getSignPassword(password,salt);
      mysql.connect();
  var sql = "INSERT INTO q_user (uname,password,ugender,uemail,urealname,salt) VALUES (?,?,?,?,?,?)" ;
      mysql.query(sql,[userName,newpass,gender,email,realName,salt],function(err,rows,fields){
        console.log(rows);
        if(rows && rows.affectedRows == 1){
          res.render('users/register',{title:uglobal.title,success:true,msg:'ok',user:user});
        } else {
          res.render('users/register',{title:uglobal.title,userName:userName,email:email,realname:realName,gender:gender,success:false,errors:[{error:err}]});
        }
        mysql.close();//异步关闭数据库
      });

  //INSERT INTO q_user (uname, password,ugender,uemail,urealname) VALUES ('Wilson', 'Champs-Elysees','1','sdsd@qq.com','sdas');
});
  //t退出登录
router.get("/logout",function(req,res,next){
      res.clearCookie('loginuid');
      res.clearCookie('loginname');
      res.send("退出成功，<a href='/'>返回主页</a>");
  });





































module.exports = router;
